package app.controller;

import app.pojo.Employee;
import app.service.EmployeeService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/")
@CrossOrigin //解决跨域名获取
class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/all")
    public List<Employee> getListEmployee(){
        return employeeService.listEmployee();
    }

    @GetMapping("/getId/id={id}")
    private Employee getId(@PathVariable("id") Integer id){
        return employeeService.queryById(id);
    }

    @PostMapping("/EditEmployee")
    private Map<String,Object> editEmployee(@RequestBody Employee employee){
        System.out.println(employee);
        HashMap<String,Object> map = new HashMap<>();
        try {
            employeeService.editEmployee(employee);
            map.put("success",true);
            map.put("msg","修改员工成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("success",false);
            map.put("msg","修改员工失败");
        }
        return map;
    }

    @GetMapping("/getUsername")
    public List<Employee> getUsername(@Param("username") String username){
        return employeeService.queryByUsername(username);
    }

    @PostMapping("/addEmployee")
    public Map<String,Object> addEmployee(@RequestBody Employee employee){
        HashMap<String,Object> map = new HashMap<>();
        try {
            employeeService.addEmployee(employee);
            map.put("success",true);
            map.put("msg","添加员工成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("success",false);
            map.put("msg","添加用户失败");
        }
        return map;
    }

    @GetMapping("/deletEmployee/{id}")
    public  Map<String,Object> deletEmployee(@PathVariable("id") Integer id){
        System.out.println(id);
        HashMap<String,Object> map = new HashMap<>();
        try {
            employeeService.deleteEmployeeById(id);
            map.put("success",true);
            map.put("msg","删除员工成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("success",false);
            map.put("msg","删除用户失败");
        }
        return map;
    }
}
